﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WhereAmiMvc.Dbs;
using WhereAmiMvc.Entities;
using WhereAmiMvc.ViewModel;

namespace WhereAmiMvc.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            var list = bll.GetAll();

            return View(list);
        }

        // GET: Employee/Details/5
        public ActionResult Details(string id)
        {
            var list = bll.GetByCondition("*", string.Format(" AND IdEmployee='{0}'", id));

            return View(list.First());
        }

        // GET: Employee/Create
        public ActionResult Create()
        {
            EmployeesOfLocation obj = new EmployeesOfLocation();

            DeptBll bllL = new DeptBll();

            var listL = bllL.GetAll();

            ViewBag.LocationList = new SelectList(listL, "DeptName", "DeptName", "--选择部门--");

            return View(obj);
        }

        // POST: Employee/Create
        [HttpPost]
        public ActionResult Create(EmployeesOfLocation employeesOfLocation)
        {
            try
            {
                // TODO: Add insert logic here
                employeesOfLocation.IdEmployee = Commons.GenerateIdByForeTag("BM");

                bll.Insert(employeesOfLocation);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Employee/Edit/5
        public ActionResult Edit(string id)
        {
            EmployeesOfLocationBll bll = new EmployeesOfLocationBll();

            var obj = bll.GetByKey(id);

            DeptBll bllL = new DeptBll();

            var listL = bllL.GetAll();

            ViewBag.LocationList = new SelectList(listL, "DeptName", "DeptName", "--选择部门--");

            return View(obj);
        }
        EmployeesOfLocationBll bll = new EmployeesOfLocationBll();
        // POST: Employee/Edit/5
        [HttpPost]
        public ActionResult Edit(EmployeesOfLocation obj)
        {
            try
            {
                // TODO: Add update logic here
                bll.Update(obj);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Employee/Delete/5
        public ActionResult Delete(string id)
        {
            var obj = bll.GetByKey(id);
            return View(obj);
        }

        // POST: Employee/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                bll.DeleteByKey(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
